package com.junglone.gateway.admin.common.core.domain;

import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;

/**
 * Description: 分页数据
 *
 * @author junglone: <a href="mailto:cjlnzb@hotmail.com">cjlnzb@hotmail.com</a>
 * @since 2021/04/14 15:01
 */
public class PageDomain {
    /**
     * 当前记录起始索引
     */
    private Integer pageNum = 0;
    /**
     * 每页显示记录数
     */
    private Integer pageSize = 10;
    /**
     * 排序列
     */
    private String orderByColumn;
    /**
     * 排序的方向 "desc" 或者 "asc".
     */
    private String isAsc;

    public Integer getPageNum() {
        return pageNum;
    }

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum - 1;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public String getOrderByColumn() {
        return orderByColumn;
    }

    public void setOrderByColumn(String orderByColumn) {
        this.orderByColumn = orderByColumn;
    }

    public String getIsAsc() {
        return isAsc;
    }

    public void setIsAsc(String isAsc) {
        this.isAsc = isAsc;
    }

    public Sort getSort() {

        if (StringUtils.isEmpty(orderByColumn)) {
            return Sort.by("id").ascending();
        }

        return Sort.by(Sort.Direction.fromString(isAsc), orderByColumn);
    }

    @Override
    public String toString() {
        return String.format("Page request [number: %d, size %d, sort: %s]", getPageNum(), getPageSize(), getSort());
    }
}
